<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="用户UID">
          <el-input v-model="form.uid" placeholder="用户UID" />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="form.phone" placeholder="手机号" />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email" placeholder="	邮箱" />
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker v-model="form.timeData" type="datetimerange" range-separator="至" start-placeholder="开始日期"
            end-placeholder="截止日期" :default-time="pickerDefaultTime" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="uid" label="用户UID" align="center" width="120" />
          <el-table-column prop="nickname" label="昵称" align="center" width="120" />
          <el-table-column prop="account_name" label="账号名称" align="center" width="120" />
          <el-table-column prop="email" label="邮箱" align="center" width="150" />
          <el-table-column prop="phone_number" label="手机号" align="center" width="120">
                        <template #default="scope">{{ scope.row.phone_area_code ? "(" + scope.row.phone_area_code + ")" : "" }}{{ scope.row.phone_number }}</template>
                    </el-table-column>
          <el-table-column prop="channel" label="注册渠道" align="center" />
          <el-table-column prop="source" label="注册来源" align="center" />
          <el-table-column prop="invite_code" label="邀请码" align="center" width="100" />
          <el-table-column prop="is_kyc" label="是否kyc" align="center" :formatter="formatterKyc" />
          <el-table-column prop="kyc_name" label="kyc名称" align="center" width="100" />
          <el-table-column prop="google_uuid" label="谷歌ID" align="center" />
          <el-table-column prop="fb_uuid" label="Facebook ID" align="center" />
          <el-table-column prop="createtime" label="注册时间" align="center" width="180" :formatter="formatterTime" />

          <el-table-column label="操作" align="center" fixed="right" width="180">
            <template #default="scope">
              <el-button type="danger" @click="handleOperate(scope.$index, scope.row, 1)">封禁</el-button>
              <el-button type="primary" @click="handleOperate(scope.$index, scope.row, 0)">解禁</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination :current-page="pageable.pageNum" :page-size="pageable.pageSize" :page-sizes="[10, 25, 50, 100]"
            :background="true" layout="total, sizes, prev, pager, next, jumper" :total="pageable.total"
            @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatterTime, exportToExcel, timestamp, pickerDefaultTime } from "@/utils/utils";
import { userListApi, userBannedApi } from "@/api/moby/user/user";

const loading = ref(false);

const tableData = ref([]);
const formData = {
  email: "",
  timeData: [],
  uid: "",
  phone: "",
};
const form = reactive({ ...formData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
const formatterKyc = (row, column, cellValue, index) => {
  let obj = {
    0: "否",
    1: "是",
    2: "审核中",
  };

  return obj[cellValue];
};

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    uid: form.uid,
    email: form.email,
    phone: form.phone,
    start_time: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end_time: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
  };
};

//用户封禁/解禁
const handleOperate = (index, row, type) => {
  ElMessageBox.confirm(
    `您是否确认${type ? "冻结" : "解冻"}此用户?`,
    "温馨提示",
    {
      type: "warning",
    }
  ).then(async () => {
    const res = await userBannedApi({ uid: row.uid, opt: type });
    getTbaleData();
    ElMessage.success(res.message);
  });
};
// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await userListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
  getTbaleData();
};
// 查询
const onQuery = async () => {
  getTbaleData();
};

// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1 });
  const res = await userListApi(data);

  let head = {
    id: "ID",
    uid: "用户UID",
    nickname: "昵称",
    account_name: "账号名称",
    email: "邮箱",
    phone_area_code: "国家区号",
    phone_number: "手机号",
    channel: "注册渠道",
    source: "注册来源",
    invite_code: "邀请码",
    google_uuid: "谷歌ID",
    fb_uuid: "Facebook ID",
    is_kyc: "是否kyc",
    kyc_name: "kyc名称",
    createtime: "注册时间",
    updatetime: "更新时间",
  };
  exportToExcel(res.data.results, head, "Mobybox-用户基本信息");
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};

// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
